package com.spn.blog.api.controller.system;

import com.spn.blog.api.common.aop.LogAnnotation;
import com.spn.blog.common.dao.pojo.LoginUser;
import com.spn.blog.common.vo.Result;
import com.spn.blog.service.login.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Api(tags = "用户模块")
@RestController
@RequestMapping("api/users")
public class UserController {

    @DubboReference(check = false)
    private SysUserService sysUserService;


    /**
     *
     * @param token 消息来自当前对象
     * @param to 消息发送对象
     * @return 返回两个用户小消息对话内容
     */
    @ApiOperation(value = "获取两个用户的聊天记录")
    @PostMapping("getHistoryMsg")
    public Result getHistoryMsg(@RequestHeader("Authorization") String token,
                                @RequestParam String to){
        return sysUserService.getHistoryMsg(token, to);
    }

    @ApiOperation(value = "获取所有用户，并返回,token用于好友搜索")
    @PostMapping("getAllFriends")
    public Result getAllFriends(@RequestHeader("Authorization") String token){
        return sysUserService.getAllFriends(token);
    }

    @ApiOperation(value = "根据token获取用户")
    @GetMapping("currentUser")
    public Result currentUser(@RequestHeader("Authorization") String token){
        //RequestHeader 从浏览器head拿取Authorization参数【token】，这个token保存在浏览器的head里面。
        //TODO：社交登录的时候这个方法出现了空指针异常
        return sysUserService.findUserByToken(token);
    }

    @ApiOperation(value = "根据token获取用户资料")
    @LogAnnotation(module="资料",operator="获取个人资料")
    @GetMapping("currentUserDetail")
    public Result currentUserDetail(@RequestHeader("Authorization") String token){
        //RequestHeader 从浏览器head拿取Authorization参数【token】，这个token保存在浏览器的head里面。
        return sysUserService.findUserDetailByToken(token);
    }

    @ApiOperation(value = "sendSms接口")
    @PostMapping("sendSms")
    public Result sendSms(@RequestParam String phone){
        return sysUserService.sendSms(phone);
    }


    @ApiOperation(value = "头像上传接口")
    @PostMapping("upload")
    public Result upload(@RequestParam MultipartFile img){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        LoginUser loginUser = (LoginUser) authentication.getPrincipal();
        String userId = loginUser.getSysUser().getId();
        String originalFilename = img.getOriginalFilename();
        byte[] bytes = null;
        try {
            bytes = img.getBytes();
        } catch (IOException e) {
            e.printStackTrace();
        }
        Map<String, Object> imgMap = new HashMap<>();
        imgMap.put("originalFilename", originalFilename);
        imgMap.put("bytes", bytes);
        return sysUserService.upload(imgMap, userId);
    }
}
